//
//  WDLoginViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/21.
//

#import "WDLoginViewController.h"
#import "WDLoginViewModel.h"
#import "WDLoginAccountView.h"
#import "WDLoginPasswordView.h"

@interface WDLoginViewController ()

@property (nonatomic, strong) WDLoginViewModel *viewModel;

@property (nonatomic, strong) UIImageView *logoImgView;

@property (nonatomic, strong) UIView *titleTagView;

@property (nonatomic, strong) UILabel *titleLabel;

/// 账号
@property (nonatomic, strong) UILabel *accountTitleLabel;

@property (nonatomic, strong) WDLoginAccountView *accountView;

/// 密码
@property (nonatomic, strong) UILabel *pwdTitleLabel;

@property (nonatomic, strong) WDLoginPasswordView *passwordView;

/// 登录按钮
@property (nonatomic, strong) UIButton *btnLogin;

/// 描述
@property (nonatomic, strong) UILabel *descLabel;


@end

@implementation WDLoginViewController

- (void)dealloc {
    debugMethod();
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [WDLoginViewModel new];
    
}

- (void)initWithSubView {
    [super initWithSubView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    [self initWithLogoImgView];
    
    [self initWithTitleTagView];
    
    [self initWithTitleLabel];
    
    [self initWithAccountTitleLabel];
    
    /// 账号
    [self initWithAccountView];

    /// 密码 标题
    [self initWithPwdTitleLabel];

    /// 密码
    [self initWithPasswordView];
    
    [self initWithBtnLogin];
    
    [self initWithDescLabel];
    
    RAC(self.viewModel.reqModel, account) = self.accountView.accountTextField.rac_textSignal;
    RAC(self.viewModel.reqModel, password) = self.passwordView.pwdTextField.rac_textSignal;

}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
    
    self.passwordView.pwdTextField.secureTextEntry = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

/// logo图片
- (void)initWithLogoImgView {
    if (!_logoImgView) {
        UIImage *image = [UIImage imageNamed:@"ic-mine-logo"];
        CGRect frame = CGRectMake((kScreenW - image.size.width) * 0.5, NAVBAR_HEIGHT_STATUS + 13, image.size.width, image.size.height);
        _logoImgView = [UIImageView createWithImageView:self.view frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            
        }];
    }
}

- (void)initWithTitleTagView {
    if (!_titleTagView) {
        CGRect frame = CGRectMake(28, _logoImgView.bottom + 84, 20, 20);
        _titleTagView = [[UIView alloc] initWithFrame:frame];
        _titleTagView.backgroundColor = [UIColor colorWithHexString:@"#1EDC7C"];
        [self.view addSubview:_titleTagView];
    }
}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(33, _titleTagView.top, kScreenW - 33 * 2, 38);
        _titleLabel = [UILabel createWithLabel:self.view frame:frame font:[UIFont systemFontOfSize:26 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.text = @"苇渡服务APP";
        }];
    }
}

- (void)initWithAccountTitleLabel {
    if (!_accountTitleLabel) {
        CGRect frame = CGRectMake(28, self.titleLabel.bottom + 40, 100, 20);
        _accountTitleLabel = [UILabel createWithLabel:self.view frame:frame font:[UIFont systemFontOfSize:15 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.text = @"账号";
        }];
    }
}

/// 账号
- (void)initWithAccountView {
    if (!_accountView) {
        CGRect frame = CGRectMake(_accountTitleLabel.left, self.accountTitleLabel.bottom + 19, kScreenW - _accountTitleLabel.left * 2, 50);
        _accountView = [[WDLoginAccountView alloc] initWithFrame:frame];
        [self.view addSubview:_accountView];
    }
}

/// 密码 标题
- (void)initWithPwdTitleLabel {
    if (!_pwdTitleLabel) {
        CGRect frame = CGRectMake(28, self.accountView.bottom + 24, 100, 20);
        _pwdTitleLabel = [UILabel createWithLabel:self.view frame:frame font:[UIFont systemFontOfSize:15 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.text = @"密码";
        }];
    }
}

/// 密码
- (void)initWithPasswordView {
    if (!_passwordView) {
        CGRect frame = self.accountView.frame;
        frame.origin.y = self.pwdTitleLabel.bottom + 19;
        _passwordView = [[WDLoginPasswordView alloc] initWithFrame:frame];
        [self.view addSubview:_passwordView];
    }
}

- (void)initWithBtnLogin {
    if (!_btnLogin) {
        CGRect frame = CGRectMake(29, self.passwordView.bottom + 38, kScreenW - 29 * 2, 50);
        _btnLogin = [UIButton createWithButton:self.view frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightRegular] target:self action:@selector(actionWithLogin:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"登录" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor colorWithHexString:@"#333333"];
            button.layer.cornerRadius = 8;
            button.layer.masksToBounds = YES;
        }];
    }
}

/// 描述
- (void)initWithDescLabel {
    if (!_descLabel) {
        CGRect frame = CGRectMake(_btnLogin.left, kScreenH - IPHONEX_BOTTOM_SPACE - 10 - 42, kScreenW - _btnLogin.left * 2, 42);
        NSString *str = @"生产管理软件账户为管理员后台分配，请联系您的部门责任人确认登录账户";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        _descLabel = [UILabel createWithLabel:self.view frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#6C7482"];
            label.numberOfLines = 0;
            label.text = str;
        }];
        [UILabel changeLineSpaceForLabel:_descLabel WithSpace:5];
    }
}


/// 登录
/// - Parameter sender: sender description
- (void)actionWithLogin:(id)sender {
    if ([objectNull(self.viewModel.reqModel.account) isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"请输入手机号!"];
        return;
    }
    if (self.viewModel.reqModel.account.length != 11) {
        [SVProgressHUD showInfoWithStatus:@"请输入11位手机号!"];
        return;
    }
    
    if ([objectNull(self.viewModel.reqModel.password) isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"请输入密码!"];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"登录中......"];
    @weakify(self);
    [self.viewModel requestWithLogin:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
//            [self getUserInfo];
            [self getWithUserRoutes];
        } else {
            NSString *tip = objectNull(respond.msg);
            if ([tip isEqualToString:@""]) {
                tip = @"密码错误，请检查后重新输入！";
            }
            [SVProgressHUD showInfoWithStatus:tip];
        }
    }];
    
}

/// 得到用户菜单权限
- (void)getWithUserRoutes {
    @weakify(self);
    [[EDSAppDelegateTool sharedInstance] requestWithUserRoutes:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self getUserInfo];
        } else {
            [SVProgressHUD showInfoWithStatus:@"获取权限失败，请重新登录！"];
        }
    }];
}

/// 获取用户信息
- (void)getUserInfo {
    [[EDSAppDelegateTool sharedInstance] requestWithUserInfo:^(EDSRespondDataEntity * _Nonnull respond) {
        [SVProgressHUD showSuccessWithStatus:@"登录成功！"];
        [[EDSAppDelegateTool sharedInstance] loginSuccess];
        [[EDSAppDelegateTool sharedInstance] updateRootVC];
        
        // 登录成功后发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:kUSER_LOGIN_SUC_NOTI object:nil];
    }];
}


@end
